from ap.connect_cli import *

CONN = dict()

def connect_to_ap(alias, host, user='root', pwd='*******', port = 17555,publickey="need"):
    ''' *函数说明*: 连接AP
        需要publickey就不传参数publickey：connect_to_ap  ap  ${ip}
        不需要publickey就任意传参数publickey:connect_to_ap  ap  ${ip}  publickey=1
    '''
    if CONN.__contains__(alias):
        CONN.pop(alias)
    conn = CONN.setdefault(alias, BackSAP())
    return conn.open_BackSAP_connection(host,user,pwd,port,publickey)

def send_command_AP(alias, cmd, timeout=None):
    conn = CONN.get(alias)
    if conn is None:
        raise Exception("Not found connection by alias '%s'" % alias)
    logger.info(cmd)
    return conn.SendCmd_ap(cmd)
    
def close_connect_ap(alias):
    conn = CONN.get(alias)
    if conn is None:
        print ("Not found connection by alias '%s'" % alias)
        return None
    conn.close_linuxAP()
    CONN.pop(alias)


